﻿// NARC.cs:
// Class for Nintendo archive file.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License.
// See LICENSE for more information.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace SharpNitroLib.NARC
{

    /// <summary>
    /// Class for Nintendo archive file.
    /// </summary>
    public class NARC
    {

		/// List of file entries in NARC.
        private readonly List<NARCFileEntry> _fileEntries = new List<NARCFileEntry>();

       	/// <summary>
       	/// Add file entry to NARC.
       	/// </summary>
       	/// <param name="narcFileEntry">File entry to add.</param>
        public void AddFileEntry(NARCFileEntry narcFileEntry)
        {
            if (null == narcFileEntry)
                return;
            if (_fileEntries.Contains(narcFileEntry))
            	return;
            
            _fileEntries.Add(narcFileEntry);
        }

        /// <summary>
        /// Remove file entry at index.
        /// </summary>
        /// <param name="index">Index of entry to remove.</param>
        public void RemoveFileEntry(int index)
        {
            if (0 > index || _fileEntries.Count < index)
                throw new Exception("Invalid index.");
            _fileEntries.RemoveAt(index);
        }

        /// <summary>
        /// Return number of files in NARC.
        /// </summary>
        public int FileCount
        {
            get
            {
                return _fileEntries.Count;
            }
        }

        /// <summary>
        /// File entries.
        /// </summary>
        public NARCFileEntry[] FileEntries
        {
            get { return _fileEntries.ToArray(); }
        }


        /// <summary>
        /// Get file data with FileId.
        /// </summary>
        /// <param name="fileId">Id of file.</param>
        /// <returns>Bytes of file.</returns>
        public byte[] GetFileData(UInt32 fileId)
        {
        	return _fileEntries[(int)fileId].Data;
        }
    }
}
